package com.cv;

import android.util.Log;

import org.webrtc.Logging;
import org.webrtc.VideoFrame;
import org.webrtc.VideoFrameBufferType;

import java.nio.ByteBuffer;

public class FaceBeauty {
    private static final String TAG = "FaceBeauty";

    static {
        String name = "facebeauty_so";
        Log.e(TAG, "Loading library: " + name);
        try {
            System.loadLibrary(name);
        } catch (UnsatisfiedLinkError e) {
            Log.e(TAG, "Failed to load native library: " + name, e);
        }
    }

    public static void beauty(VideoFrame frame, boolean beauty, int skinSoft, int skinWhite, boolean simplified,boolean mirror){
        VideoFrame.Buffer frameBuffer = frame.getBuffer();
        if(frameBuffer.getBufferType() == VideoFrameBufferType.I420){
            long startTime = System.currentTimeMillis();
            VideoFrame.I420Buffer buffer = frameBuffer.toI420();
            nativeFaceBeauty(buffer.getDataY(),buffer.getDataU(),buffer.getDataV(),
                             buffer.getStrideY(),buffer.getStrideU(),buffer.getStrideV(),
                             buffer.getWidth(),buffer.getHeight(),
                    beauty,skinSoft,skinWhite,simplified,mirror);
            buffer.release();
            //Log.e(FaceBeauty.class.getSimpleName(),"--------FaceBeauty:"+(System.currentTimeMillis() - startTime) + "ms");
        }else {
            //Log.e(FaceBeauty.class.getSimpleName(),"--------frame not I420:"+frameBuffer.getBufferType());
        }
    }

    private native static void nativeFaceBeauty(ByteBuffer y,ByteBuffer u, ByteBuffer v, int stride_y, int stride_u, int stride_v, int width, int height, boolean beauty, int skinSoft, int skinWhite,boolean simplified,boolean mirror);
}
